#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>

static dev_t dev_num;

struct cdev cdev_test;
struct file_operations cdev_ops={
    .owner=THIS_MODULE
};

static int param_init(void)
{
    int ret = 0;
    int major = 0,minor = 0;
    ret = alloc_chrdev_region(&dev_num,0,1,"chrdev_name");
    if (ret < 0)
    {
        printk("alloc_chrdev_region error\n");
    }
    printk("alloc_chrdev_region ok\n");
    major = MAJOR(dev_num);
    minor = MINOR(dev_num);
    printk("major is %d\n",major);
    printk("minor is %d\n",minor);

    cdev_test.owner = THIS_MODULE;
    cdev_init(&cdev_test,&cdev_ops);
    
    ret = cdev_add(&cdev_test,dev_num,1);
    if (ret < 0)
    {
        printk("cdev_add error\n");
    }
    printk("cdev_add ok\n");
    return 0;
}

static void param_exit(void)
{
    cdev_del(&cdev_test);
    unregister_chrdev_region(dev_num,1);

    printk("module bye!\n");
}

module_init(param_init);
module_exit(param_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("yan");
MODULE_VERSION("v0.1");










